require 'optparse'
require 'wpspider/importer/import_command'

module WPSpider
  module Importer

    #
    # Commandline interface for ImportCommand.
    #
    class ImportCUI

      #
      # Constructor.
      #
      def initialize()
        @command = ImportCommand.new()
        @option_parser = OptionParser.new()
        init_option_parser()
      end

      #
      # Parses commandline arguments.
      #
      def parse(args)
        begin
          @option_parser.parse!(args)
          @command.xmlfile = args.first
        rescue OptionParser::ParseError
          $stderr.puts($!)
          $stderr.puts(@option_parser.help)
        end
      end

      #
      # Invoke ImportCommand.
      #
      def start()
        @command.execute()
      end

      def init_option_parser()
        @option_parser.on('-c conffile') do |arg|
          @command.conffile = arg
        end
      end
      private :init_option_parser
    end
  end
end
